import { Controller, Get, Post, Body, Patch, Param, Delete, Query,Req, Res } from '@nestjs/common';
import { GenService } from './gen.service';
import { CreateGenDto } from './dto/create-gen.dto';
import { UpdateGenDto } from './dto/update-gen.dto';
import { QueryGenDto } from './dto/query-gen-dto';
import { Request,Response } from 'express';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RequirePermission } from 'src/decorator/reuqire-permission';
import { Public } from 'src/decorator/public';

@Controller('gen')
@ApiTags('代码生成')
@ApiBearerAuth()
export class GenController {
  constructor(private readonly genService: GenService) {}

  @Post()
  @ApiOperation({summary:'导入到gen_table表'})
  @RequirePermission("tool:gen:import")
  create(@Body() createGenDto: CreateGenDto,@Req() req:Request) {
    return this.genService.create(createGenDto,req);
  }

  @Get()
  @ApiOperation({summary:'代码生成的表列表'})
  @RequirePermission("tool:gen:list")
  findAll(@Query()  query: QueryGenDto) {
    return this.genService.findAll(query);
  }

  @Patch(':id')
  @ApiOperation({summary:'修改代码生成的表'})
  @RequirePermission("tool:gen:edit")
  update(@Param('id') id: string, @Body() updateGenDto: UpdateGenDto,@Req() req:Request) {
    return this.genService.update(+id, updateGenDto,req);
  }

  @Post('/del')
  @ApiOperation({summary:'删除代码生成的表'})
  @RequirePermission("tool:gen:remove")
  remove(@Body() dto: {ids:number[]}) {
    return this.genService.remove(dto.ids);
  }

  @Get('db-list')
  @ApiOperation({summary:'根据查询条件查询数据库中所有的表'})
  @RequirePermission("tool:gen:getDBTable")
  getDBTableInformation(@Query()  query: QueryGenDto) {
    return this.genService.getDBTableNames(query);
  }

  @Get('table/column-info')
  @ApiOperation({summary:'根据table_id获取数据表的信息、字段列表'})
  @RequirePermission("tool:gen:getDBColumn")
  getTableColumn(@Query() query: {id:number,pageNum:number}) {
    return this.genService.getTableColumn(query);
  }

  @Get('preview')
  @ApiOperation({summary:'预览代码'})
  @RequirePermission("tool:gen:preview")
  preview(@Query('id') id: string) {
    return this.genService.preview(+id);
  }

  @Get('download')
  @ApiOperation({summary:'生成代码（压缩包）'})
  @RequirePermission("tool:gen:code")
  download(@Query('id') id: string,@Res() res: Response) {
    return this.genService.genCode(+id,res);
  }

  @Get('genTable')
  @ApiOperation({summary:'生成代码（自定义路径）'})
  @RequirePermission("tool:gen:code")
  genTable(@Query('id') id: string) {
    return this.genService.genTable(+id);
  }

  @Public()
  @Get('exportDataTable')
  @ApiOperation({summary:'导出数据表'})
  exportDataTable(@Res() res: Response) {
    return this.genService.exportDataTable(res)
  }
}
